package com.xkcyy.one.controller;

import com.xkcyy.one.ai.llm.model.ChatResult;
import com.xkcyy.one.ai.trainable.ToolAgentMessageProcessor;
import com.xkcyy.one.common.Result;
import com.xkcyy.one.dto.MessageDTO;
import com.xkcyy.one.dto.MessageRequestDTO;
import com.xkcyy.one.entity.Message;
import com.xkcyy.one.service.MessageService;

import lombok.AllArgsConstructor;
import reactor.core.publisher.Flux;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息控制器
 *
 * @author AI Assistant
 */
@RestController
@RequestMapping("/api/messages")
@AllArgsConstructor
public class MessageController {

    private final MessageService messageService;
    private final ToolAgentMessageProcessor toolAgentMessageProcessor;

    /**
     * 获取所有消息
     *
     * @return 消息列表
     */
    @GetMapping("/list")
    public Result<List<MessageDTO>> getAllMessages() {
        return Result.success(messageService.getAllMessages());
    }

    /**
     * 根据ID获取消息
     *
     * @param id 消息ID
     * @return 消息信息
     */
    @GetMapping("/get")
    public Result<MessageDTO> getMessageById(@RequestParam Long id) {
        return Result.success(messageService.getMessageById(id));
    }

    /**
     * 根据对话ID获取消息列表
     *
     * @param conversationId 对话ID
     * @return 消息列表
     */
    @GetMapping("/conversation")
    public Result<List<MessageDTO>> getMessagesByConversationId(@RequestParam Long conversationId) {
        return Result.success(messageService.getMessagesByConversationId(conversationId));
    }

    /**
     * 创建消息
     *
     * @param message 消息信息
     * @return 创建后的消息信息
     */
    @PostMapping("/create")
    public Result<MessageDTO> createMessage(@RequestBody Message message) {
        return Result.success(messageService.createMessage(message));
    }

    /**
     * 处理用户消息并生成AI回复
     *
     * @param messageRequest 消息请求
     * @return AI回复消息
     */
    @PostMapping("/chat")
    public Result<MessageDTO> chat(@RequestBody MessageRequestDTO messageRequest) {
        return Result.success(messageService.processUserMessage(messageRequest));
    }

    /**
     * 处理用户消息并生成流式AI回复（一个字一个字返回）
     *
     * @param messageRequest 消息请求
     * @return SSE流式响应
     */
    @PostMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatResult> chatStream(@RequestBody MessageRequestDTO messageRequest) {
        return messageService.chatSteam(messageRequest);
    }
    
    /**
     * 处理用户消息并生成支持工具调用的流式AI回复
     *
     * @param messageRequest 消息请求
     * @return 带工具调用的SSE流式响应
     */
    @PostMapping(value = "/chat/tools/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatResult> chatStreamWithTools(@RequestBody MessageRequestDTO messageRequest) {
        return toolAgentMessageProcessor.chatSteamWithTools(messageRequest);
    }

    /**
     * 更新消息
     *
     * @param id      消息ID
     * @param message 消息信息
     * @return 更新后的消息信息
     */
    @PutMapping("/update")
    public Result<MessageDTO> updateMessage(@RequestParam Long id, @RequestBody Message message) {
        message.setId(id);
        return Result.success(messageService.updateMessage(message));
    }

    /**
     * 删除消息
     *
     * @param id 消息ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Result<Void> deleteMessage(@RequestParam Long id) {
        messageService.deleteMessage(id);
        return Result.success();
    }
}
